import request from '../../utils/request'
import PubSub from 'pubsub-js'
import moment from 'moment'
const appInstance = getApp()
// pages/songDetail/songDetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isPlay:false,
    song:{},
    musicId:'',
    musicLink:'',
    currentTime:'00:00',
    durationTime:'00:00', //总时长
    currentWidth:0
  },
  //点击切歌的按钮
  handleSwitch(event){
    let type = event.currentTarget.id;
    // 关闭当前播放的音乐
    this.backgroundAudioManager.stop()
    PubSub.publish('switchType', type)
    PubSub.subscribe('musicId', (msg, musicId) => {
      console.log(musicId);
      // 获取音乐详情信息
      this.getMusicInfo(musicId)
      //点击下一首后自动播放
      this.musicControl(true, musicId)
      PubSub.unsubscribe('musicId')
    })
  },
  // 点击播放暂停的回调
  handleMusicPlay(){
    let isPlay = !this.data.isPlay;
    this.setData({
      isPlay
    })
    let {musicId, musicLink} =this.data;
    this.musicControl(isPlay, musicId, musicLink)
  },
  // 控制音乐播放暂停的功能函数
  async musicControl(isPlay, musicId, musicLink){
    
    if (isPlay) {
      if (!musicLink) {
        let musicLinkData = await request('/song/url', {id: musicId});
        musicLink = musicLinkData.data[0].url;

        this.setData({
          musicLink
        })
      }


      this.backgroundAudioManager.src=musicLink;
      this.backgroundAudioManager.title=this.data.song.name;
    }else{
      this.backgroundAudioManager.pause()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let musicId = options.musicId;
    this.setData({
      musicId
    })
    this.getMusicInfo(musicId);
    // 判断当前页面音乐是否在播放
    if (appInstance.globalData.isMusicPlay && appInstance.globalData.musicId ===musicId) {
      this.setData({
        isPlay:true
      })
    }
    this.backgroundAudioManager = wx.getBackgroundAudioManager();
    // 监视音乐的播放暂停
    this.backgroundAudioManager.onPlay(() =>{
      this.changeAudioPlay(true)
      // 修改全局音乐播放的状态
      appInstance.globalData.musicId = musicId;
    })
    this.backgroundAudioManager.onPause(() => {
      this.changeAudioPlay(false)
      
    })
    this.backgroundAudioManager.onStop(() => {
      this.changeAudioPlay(false)
      
    })
    this.backgroundAudioManager.onEnded(() => {
      // 自动切换下一首音乐，发送一个next事件
      PubSub.publish('switchType', 'next')
      //将实施进度条还原成0
      this.setData({
        currentWidth:0,
        currentTime:'00:00'
      })
      
    })
    // 监听音乐实时播放的进度
    this.backgroundAudioManager.onTimeUpdate(() => {
      let currentTime = moment(this.backgroundAudioManager.currentTime * 1000).format('mm:ss')
      // 用比例来计算当前播放宽度，当前时长/总时长 = 当前宽度/总宽度，已知三个求一个
      let currentWidth = this.backgroundAudioManager.currentTime/this.backgroundAudioManager.duration * 450;

      this.setData({
        currentTime,
        currentWidth
      })
    })
  },
  // 封装更新状态的函数
  changeAudioPlay(isPlay){
    this.setData({
      isPlay
    })
    appInstance.globalData.isMusicPlay = isPlay;
  },
  // 获取音乐详情的功能函数
  async getMusicInfo(musicId){
    let songData = await request('/song/detail',{ids:musicId})

    let durationTime = moment(songData.songs[0].dt).format('mm:ss')
    this.setData({
      song:songData.songs[0],
      durationTime
    })
    wx.setNavigationBarTitle({
      title:this.data.song.name
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})